#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setGeometry(0,0,800,480);

    tcpSocket = new QTcpSocket(this);

    button[0] = new QPushButton(QString("连接服务器"));
    button[1] = new QPushButton(QString("断开连接"));
    button[2] = new QPushButton(QString("清空文本"));
    button[3] = new QPushButton(QString("发送消息"));
    label[0] = new QLabel(QString("服务器地址："));
    label[1] = new QLabel(QString("服务器端口："));

    browser = new QTextBrowser;
    lineEdit = new QLineEdit(QString("abandon客户端"));
    hLayout[0] = new QHBoxLayout;
    hLayout[1] = new QHBoxLayout;
    hLayout[2] = new QHBoxLayout;
    vLayout = new QVBoxLayout(this);

    button[1]->setEnabled(false);

    comboBox = new QComboBox;
    spinBox = new QSpinBox;
    spinBox->setRange(10000,99999);

    getLocalHostIP();
    connectSigsToSlots();

    setupUi();
}

Widget::~Widget() {}

void Widget::setupUi()
{
    hLayout[0]->addWidget(lineEdit);
    hLayout[0]->addWidget(button[3]);

    hLayout[1]->addWidget(button[0]);
    hLayout[1]->addWidget(button[1]);
    hLayout[1]->addWidget(button[2]);

    label[0]->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    label[1]->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    hLayout[2]->addWidget(label[0]);
    hLayout[2]->addWidget(comboBox);
    hLayout[2]->addWidget(label[1]);
    hLayout[2]->addWidget(spinBox);

    vLayout->addWidget(browser);
    vLayout->addLayout(hLayout[2]);
    vLayout->addLayout(hLayout[1]);
    vLayout->addLayout(hLayout[0]);

}

void Widget::getLocalHostIP()
{
    //获取所有接口
    QList<QNetworkInterface> inteList = QNetworkInterface::allInterfaces();
    for(int i=0; i < inteList.size(); i++)
    {
        //获取地址列表
        QList<QNetworkAddressEntry> addList = inteList[i].addressEntries();
        for(int i=0; i<addList.size();i++)
        {
            if(addList[i].ip().protocol()==QHostAddress::IPv4Protocol)
            {
                comboBox->addItem(QString(addList[i].ip().toString()));
                list<<addList[i].ip();
            }

        }
    }
}

void Widget::connectSigsToSlots()
{
    connect(button[0],&QPushButton::clicked,this,&Widget::toConnect);
    connect(button[1],&QPushButton::clicked,this,&Widget::toDisConnect);
    connect(button[2],&QPushButton::clicked,this,&Widget::clearTextBrowser);
    connect(button[3],&QPushButton::clicked,this,&Widget::sendMessage);
    connect(tcpSocket,&QTcpSocket::connected,this,&Widget::connected);
    connect(tcpSocket,&QTcpSocket::disconnected,this,&Widget::disConnected);
    connect(tcpSocket,&QTcpSocket::readyRead,this,&Widget::receiveMessage);
    connect(tcpSocket,&QTcpSocket::stateChanged,this,&Widget::socketStateChanged);
}

void Widget::toConnect()
{
    if(tcpSocket->state()!=QAbstractSocket::ConnectedState)
    {
        tcpSocket->connectToHost(list[comboBox->currentIndex()],spinBox->value());
    }
}

void Widget::toDisConnect()
{
    if(tcpSocket->state()==QAbstractSocket::ConnectedState)
    {
        tcpSocket->disconnectFromHost();
        tcpSocket->close();
    }
}

void Widget::connected()
{
    browser->append(QString("已连接上服务器"));
    button[0]->setEnabled(false);
    button[1]->setEnabled(true);
    comboBox->setEnabled(false);
    spinBox->setEnabled(false);
}

void Widget::disConnected()
{
    browser->append(QString("服务器断开连接"));
    button[0]->setEnabled(true);
    button[1]->setEnabled(false);
    comboBox->setEnabled(true);
    spinBox->setEnabled(true);
}

void Widget::clearTextBrowser()
{
    browser->clear();
}

void Widget::receiveMessage()
{
    QString message=tcpSocket->readAll();
    browser->append(QString("服务器：")+message);
}

void Widget::sendMessage()
{
    if(tcpSocket==nullptr)
    {
        return;
    }
    if(tcpSocket->state()==QAbstractSocket::ConnectedState)
    {
        tcpSocket->write(lineEdit->text().toUtf8());
        browser->append(QString("客户端：")+QString(lineEdit->text().toUtf8().data()));
    }
}

void Widget::socketStateChanged(QAbstractSocket::SocketState state)
{
    switch(state)
    {
    case QAbstractSocket::ConnectedState:
        browser->append(QString("Socket状态：ConnectedState"));
        break;
    case QAbstractSocket::ConnectingState:
        browser->append(QString("Socket状态：ConnectingState"));
        break;
    case QAbstractSocket::UnconnectedState:
        browser->append(QString("Socket状态：UnconnectedState"));
        break;
    case QAbstractSocket::HostLookupState:
        browser->append(QString("Socket状态：HostLookupState"));
        break;
    case QAbstractSocket::ClosingState:
        browser->append(QString("Socket状态：ClosingState"));
        break;
    case QAbstractSocket::ListeningState:
        browser->append(QString("Socket状态：ListeningState"));
        break;
    case QAbstractSocket::BoundState:
        browser->append(QString("Socket状态：BoundState"));
        break;
    default:
        break;
    }
}
